declare let eManager;
const {ccclass, property} = cc._decorator;

//3-1 擦鼻影
@ccclass
export default class CaBiZi extends cc.Component {
    @property
    stepName = "";
    //刷子1 
    @property(cc.Node)
    shuaZi1: cc.Node = null;
    //刷子2 
    @property(cc.Node)
    shuaZi2: cc.Node = null;
    //刷子晃动1 
    @property(sp.Skeleton)
    shuaZiSk1: sp.Skeleton = null;
    //刷子晃动2 
    @property(sp.Skeleton)
    shuaZiSk2: sp.Skeleton = null;

    //刷子碰撞区1
    @property(cc.Node)
    shuaPengNode1: cc.Node = null;

    //刷子碰撞区2
    @property(cc.Node)
    shuaPengNode2: cc.Node = null;

    //鼻子碰撞区
    @property(cc.Node)
    pengNode: cc.Node = null;
    
    //鼻子阴影动画
    @property(cc.Animation)
    biZiSkAni1: cc.Animation = null;

    //鼻子高光动画
    @property(cc.Animation)
    biZiSkAni2: cc.Animation = null;

    @property(cc.Node)
    done_effect1: cc.Node = null;

    @property(cc.Node)
    done_effect2: cc.Node = null;

    //虚线 
    @property(cc.Node)
    xuxianNode: cc.Node = null;

    //音效
    @property({type:cc.AudioClip})
    biYingSound: cc.AudioClip = null;

    @property({type:cc.AudioClip})
    gaoGuangSound: cc.AudioClip = null;
    

    _isPlay = false;
    _curPoint = 1;//一共2步  先是鼻影 再是高光

    start(){
        this.biZiSkAni1.on("finished",this.playFinish1,this);
        this.biZiSkAni2.on("finished",this.playFinish2,this);
    };
    //初始化
    initData(){
        this._curPoint = 1;
        // this.gaoGuangTips.active = false;
        this.shuaZi1.active = true;
        this.shuaZi2.active = false;
        this.biZiSkAni1.node.active = false;
        this.biZiSkAni2.node.active = false;
        this._isPlay = false;
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", "step_3_1_1");
    };

    _touchStart(event:any){
        let worldPoint = event.touch.getLocation();
        if(this._curPoint == 1){
            this.shuaZiSk1.setAnimation(0,"animation",true);
            let localPoint = this.shuaZiSk1.node.parent.convertToNodeSpaceAR(worldPoint);
            this.shuaZiSk1.node.x = localPoint.x;
            this.shuaZiSk1.node.y = localPoint.y;
        }else{
            this.shuaZiSk2.setAnimation(0,"animation",true);
            let localPoint = this.shuaZiSk2.node.parent.convertToNodeSpaceAR(worldPoint);
            this.shuaZiSk2.node.x = localPoint.x;
            this.shuaZiSk2.node.y = localPoint.y;
        }
    };

    _touchMove(event:any){
        if(this._isPlay)
            return;
        if(this._curPoint > 2)
            return;
        // let delta = event.touch.getDelta();
        // if(this._curPoint == 1){
        //     this.shuaZi1.x += delta.x;
        //     this.shuaZi1.y += delta.y;
        // }else{
        //     this.shuaZi2.x += delta.x;
        //     this.shuaZi2.y += delta.y;
        // }
        let worldPoint = event.touch.getLocation();
        if(this._curPoint == 1){
            let localPoint = this.shuaZiSk1.node.parent.convertToNodeSpaceAR(worldPoint);
            this.shuaZiSk1.node.x = localPoint.x;
            this.shuaZiSk1.node.y = localPoint.y;
        }else{
            let localPoint = this.shuaZiSk2.node.parent.convertToNodeSpaceAR(worldPoint);
            this.shuaZiSk2.node.x = localPoint.x;
            this.shuaZiSk2.node.y = localPoint.y;
        }
        let peng = this.pengNode.getBoundingBoxToWorld();
        let box = this._curPoint == 1 ? this.shuaPengNode1.getBoundingBoxToWorld() : this.shuaPengNode2.getBoundingBoxToWorld();
        if (peng.intersects(box)) {
            this.playAni();
        }
    };

    _touchEnd(event:any){
        if(this._curPoint == 1){
            this.shuaZiSk1.setAnimation(0,"moren",false);
        }else{
            this.shuaZiSk2.setAnimation(0,"moren",false);
        }
    };

    //动画
    playAni() {
        this._isPlay = true;
        if(this._curPoint == 1){
            this.biZiSkAni1.node.active = true;
            this.shuaZi1.active = false;
            this.biZiSkAni1.play();
            eManager.emit("SET_VIBRATE", 1500);
            this.scheduleOnce(()=>{
                cc.audioEngine.play(this.biYingSound, false, 2);
            },1);
            cc.audioEngine.play(this.biYingSound, false, 2);
        }else{
            this.biZiSkAni2.node.active = true;
            this.shuaZi2.active = false;
            this.biZiSkAni2.play();
            eManager.emit("SET_VIBRATE", 1000);
            cc.audioEngine.play(this.gaoGuangSound, false, 2);
        }
    };

    playFinish(){
        this._isPlay = false;
    };
    playFinish1(){
        this._isPlay = false;
        this._curPoint ++;
        this.biZiSkAni1.node.active = false;
        this.shuaZi2.active = true;
        this.done_effect1.active = true;
        eManager.emit("HZ_REFRESH_ICON_TIPS", null);
    };
    playFinish2(){
        this._isPlay = false;
        this._curPoint ++;
        this.biZiSkAni2.node.active = false;
        this.done_effect2.active = true;
        this.xuxianNode.active = false;
        eManager.emit("DONE_POINT", 1);
        eManager.emit("MINI_DONE_POINT", 2);
        eManager.emit("HZ_MAKE_DOT", {
            makeup_close:3,
            makeup_step:this.stepName
        });
    };
}
